package org.elucid.chess.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 */
public class Board {

    public static final int ROWS = 8;
    public static final int COLUMNS = 8;
    private List<Square> squares;

    public enum Direction {

        North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest
    }

    /**
     * 
     */
    public Board() {
        super();
        setSquares(new ArrayList<Square>());

        for (int i = ROWS - 1; i >= 0; i--) {
            for (int j = 0; j < COLUMNS; j++) {
                getSquares().add(new Square(i, j, (i % 2 + j % 2 == 1)));
            }
        }
    }


    /**
     * 
     * @param row
     * @param column
     * @return
     */
    public Square getSquareAt(int row, int column) {
        for (Square square : squares) {
            if (square.getRow() == row && square.getColumn() == column) {
                return square;
            }
        }
        return null;
    }
    
    /**
     * 
     * @param fromSquare
     * @param direction
     * @param distance
     * @return
     */
    public Square getSquareAt(Square fromSquare, Direction direction, int distance) {
        int row = fromSquare.getRow();
        int column = fromSquare.getColumn();
        switch(direction)
        {
            case North: row = row + distance; break;
            case NorthEast: row = row + distance; column = column + distance; break;
            case East: column = column + distance; break;
            case SouthEast: row = row - distance; column = column + distance; break;
            case South: row = row - distance; break;
            case SouthWest: row = row - distance;  column = column - distance;break;
            case West: column = column - distance; break;
            case NorthWest: row = row + distance; column = column - distance; break;
        }
        return getSquareAt( row, column );
    }
    

    public void setSquares(List<Square> squares) {
        this.squares = squares;
    }

    public List<Square> getSquares() {
        return squares;
    }
}
